package com.face.server.controller;

import com.face.RespMsg;
import com.face.server.entity.Dormitory;
import com.face.server.entity.DormitoryAdmin;
import com.face.server.entity.Student;
import com.face.server.repository.DormAdminRepository;
import com.face.server.service.CSVService;
import com.face.server.service.DormAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.security.Principal;

/**
 * Created by ryan on 2018/1/26.
 */
@RestController
@RequestMapping("/dormAdmin")
public class DorAdminController {
    @Autowired
    private DormAdminService dormAdminService;

    @Autowired
    private DormAdminRepository dormAdminRepository;

    @Autowired
    CSVService csvService;

    @GetMapping("/list")
    public RespMsg search(@PageableDefault(sort = "dormitoryBuildNum",size = 10000) Pageable pageable, @RequestParam(value = "key1",required = false) String key1, @RequestParam(value = "key2",required = false) String key2){
        return RespMsg.build(dormAdminService.search(pageable,key1,key2));
    }

    @GetMapping("/findOne{id}")
    public RespMsg findOne(@PathVariable String id){
        return RespMsg.build(dormAdminRepository.findOne(id));
    }

    @GetMapping("/listMy")
    public RespMsg listMy(@PageableDefault(sort = "dormitoryBuildNum",size = 10000)Pageable pageable, @RequestParam(value = "key1",required = false)String key1, @RequestParam(value = "key2",required = false)String key2, Principal principal){
        return RespMsg.build(dormAdminService.search(pageable,key1,key2,principal));
    }

    @PutMapping("/update")
    public RespMsg update(@RequestBody @NotNull DormitoryAdmin dormitoryAdmin) {
        if (dormitoryAdmin.getId() == null) {
            return RespMsg.error("对不起，id不能为空");
        }
        return RespMsg.build(dormAdminService.update(dormitoryAdmin));
    }

    @DeleteMapping("/delete/{id}")
    public RespMsg delete(@PathVariable String id){
        return RespMsg.build(dormAdminService.delete(id));
    }

    @PostMapping("/create")
    public RespMsg create(@RequestBody @NotNull DormitoryAdmin dormitoryAdmin){
        return RespMsg.build(dormAdminService.create(dormitoryAdmin));
    }

    @PutMapping("/importDormAdmin")
    RespMsg put(MultipartFile file) throws IOException {
        return file == null ?
                RespMsg.error("对不起，文件不能为空") :
                csvService.parseDormAdmin(file);
    }

    @PutMapping("/updateFace/{id}/{fid}")
    RespMsg updateFace(@PathVariable String id,@PathVariable String fid,@RequestParam MultipartFile file) throws IOException {
        return file==null?
                RespMsg.error("对不起，文件不能为空"):
                dormAdminService.updateFace(id,fid,file);
    }

    @PutMapping("/addFace/{id}")
    RespMsg addFace(@PathVariable String id,@RequestParam MultipartFile file) throws IOException {
        return file==null?
                RespMsg.error("对不起，文件不能为空"):
                dormAdminService.addFace(id,file);
    }
}
